﻿using ComLib.ComType;
using KXmlUIControl;
using System;
using System.Collections.Generic;

namespace KFormBase
{
    public class PageManager : Singleton<PageManager>
    {    
        private Dictionary<string, ModulePage> _dicPages = new Dictionary<string, ModulePage>();
        
        /// <summary>
        /// 最新错误信息
        /// </summary>
        public string Error { get; private set; }

        public PageManager()
        {

        }

        public bool Init(List<XmlPageObject> listConfigPages)
        {
            for (int i = 0; i < listConfigPages.Count; i++)
            {
                XmlPageObject pageConfig = listConfigPages[i];
                if (!_dicPages.ContainsKey(pageConfig.TypeName))
                {
                    ModulePage newPage = new ModulePage(pageConfig, i+1);
                    if(newPage.Initialize())
                    {
                        _dicPages.Add(pageConfig.TypeName, newPage);
                    }
                    else
                    {
                        Error = newPage.Error;
                        return false;
                    }
                }
            }
            return true;
        }

        public ModulePage this[string key]
        {
            get 
            {
                if (_dicPages.ContainsKey(key))
                {
                    return _dicPages[key];
                }
                else
                {
                    throw new Exception("Cannot find the module page:" + key);
                }
            }
        }
    }
}
